package com.cndw.kungfu.business.gm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.domain.Mail;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.UtilsOfBusiness;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.message.MailManager;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessMail {
	@Autowired
	private MailManager mailManager;

	@UnAuth
	@GmCommand
	public ResponseMessage sendMail(Session session, String sign, Map<String, Object> paramMap) {

		String title = paramMap.get("title").toString();
		String content = paramMap.get("content").toString();
		int type = Integer.parseInt(paramMap.get("userType").toString());
		String users = paramMap.get("users").toString();

		if (BlankUtils.isBlank(title) || BlankUtils.isBlank(content)) {
			return ResponseMessage.errMsg(LanguageLib.get("comm.err.args"));
		}

//		int titleLength = title.getBytes(Constants.charset).length;
//		if (titleLength < ConstantsConfig.getMailTitleLength() || titleLength > ConstantsConfig.getMailTitleLengthMax()) {
//			return ResponseMessage.errMsg(LanguageLib.get("mail.err.title"));
//		}
//
//		content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "");
//		int contentLength = content.getBytes(Constants.charset).length;
//		if (contentLength < ConstantsConfig.getMailContentLength() || contentLength > ConstantsConfig.getMailContentLengthMax()) {
//			return ResponseMessage.errMsg(LanguageLib.get("mail.err.content"));
//		}
//
//		String bad = WordProcesser.chkBadWorlds(title);
//		if (!BlankUtils.isBlank(bad)) {
//			return ResponseMessage.errMsg(LanguageLib.get("mail.err.bad", bad));
//		}
//
//		bad = WordProcesser.chkBadWorlds(content);
//		if (!BlankUtils.isBlank(bad)) {
//			return ResponseMessage.errMsg(LanguageLib.get("mail.err.bad", bad));
//		}

		// 统计接收用户，和不存在的用户
		List<String> failList = new ArrayList<String>();
		List<Player> players = new ArrayList<Player>();
		if (type == 1) {
			for (String user : users.split(",")) {
				Player player = UtilsOfBusiness.getPlayer(type, user);
				if (!BlankUtils.isBlank(player)) {
					players.add(player);
				} else {
					failList.add(user);
				}
			}
		} else if (type == 2) {
			for (String user : users.split(",")) {
				long playerId = UtilsOfBusiness.getPlayerId(type, user);
				if (playerId != 0) {
					players.add(Context.getPlayerServer().getByPlayerId(playerId));
				} else {
					failList.add(user);
				}
			}
		} else if (type == 3) {
			for (String user : users.split(",")) {
				long playerId = UtilsOfBusiness.getPlayerId(type, user);
				if (playerId != 0) {
					players.add(Context.getPlayerServer().getByPlayerId(playerId));
				} else {
					failList.add(user);
				}
			}
		} else {
			return ResponseMessage.errMsg(LanguageLib.get("comm.err.args"));
		}

		if (!players.isEmpty()) {
			for (Player player : players) {
				Context.getMessageServer().sendSysMail(player.getPlayerId(), player.getPlayerName(), title, content);
			}
		}

		return ResponseMessage.getSuccMessage(failList);
	}
	
	@UnAuth
	@GmCommand
	public ResponseMessage getMails(Session session, String sign, Map<String, Object> paramMap) {
		int style = Integer.parseInt(paramMap.get("playerType").toString());
		String user = (String) paramMap.get("player");
		long playerId = UtilsOfBusiness.getPlayerId(style, user);
		
		int page = paramMap.containsKey("page")? Integer.valueOf(paramMap.get("page").toString()):0;
		int pageSize = paramMap.containsKey("pageSize")? Integer.valueOf(paramMap.get("pageSize").toString()):0;
		int action = paramMap.containsKey("actionType")? Integer.valueOf(paramMap.get("actionType").toString()):0;
		int haveAtt = paramMap.containsKey("haveAtt")? Integer.valueOf(paramMap.get("haveAtt").toString()):0;
		int gotAtt = paramMap.containsKey("gotAtt")? Integer.valueOf(paramMap.get("gotAtt").toString()):0;
		page = page <= 0 ? 1 : page;
		pageSize = pageSize == 0 ? Constants.PAGE_SIZE : pageSize;
		
		
		List<Mail> list = mailManager.getMailsGm(playerId, action, haveAtt>0, gotAtt>0, (page - 1) * pageSize, pageSize);
		int counts = mailManager.getCountGm(playerId, action, haveAtt>0, gotAtt>0);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("counts", counts);

		map.put("action", action);
		map.put("page", page);
		map.put("pageSize", pageSize);
		
		return ResponseMessage.getSuccMessage(map);
	}
}
